// $Id$
/*
 *  Copyright (C) 2007 Noury Solutions
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 3 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */
/*! \file
 * \brief Implementation of disposable collections.
 *
 * \author $Author$
 * \date $Date$
 * \version $Revision$
 */

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Salamanca.Collections
{



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>Implements a read-only <see cref="IDisposable">disposable</see> collection of <see cref="IDisposable">disposable</see> items.</summary>
    ///
    ///////////////////////////////////////////////////////////////////////////

    public class ReadOnlyDisposableCollection<TDisposable>:
        ReadOnlyCollection<TDisposable>,
        IDisposable
        where TDisposable:
            IDisposable
    {

        /// <summary>Initializes a new instance of the <see cref="ReadOnlyDisposableCollection{TDisposable}" /> class that is a read-only wrapper around the specified list.</summary>
        /// <param name="list">The list to wrap.</param>
        public ReadOnlyDisposableCollection(IList<TDisposable> list):
            base(list)
        {
        }

        /// <summary>Finalizer.</summary>
        ~ReadOnlyDisposableCollection()
        {
            Dispose(false);
        }

        /// <summary>Destructor.</summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>Releases all the resources of the <see cref="ReadOnlyDisposableCollection{TDisposable}" /> items.</summary>
        protected virtual void Dispose(bool disposing)
        {
            List<Exception> exceptions=null;

            foreach (IDisposable d in this)
            {
                try
                {
                    d.Dispose();
                } catch (Exception ex)
                {
                    if (exceptions==null)
                        exceptions=new List<Exception>();
                    exceptions.Add(ex);
                }
            }

            if (exceptions!=null)
                throw new ExceptionCollection(exceptions, Resources.SR.ExceptionsHaveBeenThrownExceptionsMessage);
        }

    }



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>Implements a <see cref="IDisposable">disposable</see> collection of <see cref="IDisposable">disposable</see> items.</summary>
    ///
    ///////////////////////////////////////////////////////////////////////////

    public class DisposableCollection<TDisposable>:
        Collection<TDisposable>,
        IDisposable
        where TDisposable:
            IDisposable
    {

        /// <summary>Initializes a new instance of the <see cref="DisposableCollection{TDisposable}" /> class.</summary>
        public DisposableCollection():
            base()
        {
        }

        /// <summary>Initializes a new instance of the <see cref="DisposableCollection{TDisposable}" /> class as a wrapper around the specified list.</summary>
        /// <param name="list">The list to wrap.</param>
        public DisposableCollection(IList<TDisposable> list):
            base(list)
        {
        }

        /// <summary>Finalizer.</summary>
        ~DisposableCollection()
        {
            Dispose(false);
        }

        /// <summary>Destructor.</summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>Releases all the resources of the <see cref="DisposableCollection{TDisposable}" /> items.</summary>
        protected virtual void Dispose(bool disposing)
        {
            List<Exception> exceptions=null;

            foreach (IDisposable d in this)
            {
                try
                {
                    d.Dispose();
                } catch (Exception ex)
                {
                    if (exceptions==null)
                        exceptions=new List<Exception>();
                    exceptions.Add(ex);
                }
            }

            if (exceptions!=null)
                throw new ExceptionCollection(exceptions, Resources.SR.ExceptionsHaveBeenThrownExceptionsMessage);
        }

    }
}
